//package controller;
//
//import com.alibaba.fastjson.JSONObject;
//import model.User;
//import service.UserService;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//import java.util.Properties;
//import java.util.UUID;
//
//public class WxLoginController extends HttpServlet {
//    private UserService userService = new UserService();
//
//    @Override
//    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        HttpSession session = req.getSession();
//        String code = req.getParameter("code");
//        Properties prop = new Properties();
//        prop.load(this.getClass().getClassLoader().getResourceAsStream("config.properties"));
//        String appid = prop.getProperty("wx.AppID");
//        String AppSecret = prop.getProperty("wx.AppSecret");
//        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + AppSecret + "&code=" + code + "&grant_type=authorization_code";
//        JSONObject authInfo = auth(url);
//        System.out.println("authInfo: " + authInfo);
//        url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + authInfo.getString("access_token") + "&openid=" + authInfo.getString("openid");
//        JSONObject userInfo = getUserInfo(url);
//        System.out.println("userInfo: " + userInfo);
//        User user = userService.findByWxOpenid(info.getString("openid"));
//        if (user == null) {
//            user = new User();
//            user.setPic(userInfo.getString("headimgurl"));
//            user.setGender(userInfo.getString("sex"));
//            user.setRealName(userInfo.getString("nickname"));
//            user.setUsername(UUID.randomUUID().toString().substring(36 - 15));
//            user.setWxOpenid(userInfo.getString("openid"));
//            userService.save(user);
//        }
//        userService.updateLoginTime(user.getId());
//        session.setAttribute("loginUser", user);
//        resp.sendRedirect("/html/home.jsp");
//    }
//
//    private JSONObject auth(String url) {
//        try {
//            CloseableHttpClient client = HttpClients.createDefault();
//            HttpGet httpGet = new HttpGet(url);
//            HttpResponse response = client.execute(httpGet);
//            HttpEntity entity = response.getEntity();
//            if (entity != null) {
//                String result = EntityUtils.toString(entity, Charset.forName("UTF8"));
//                return JSONObject.parseObject(result);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//    private JSONObject getUserInfo(String url) {
//        try {
//            CloseableHttpClient client = HttpClients.createDefault();
//            HttpGet httpGet = new HttpGet(url);
//            HttpResponse response = client.execute(httpGet);
//            HttpEntity entity = response.getEntity();
//            if (entity != null) {
//                String result = EntityUtils.toString(entity, Charset.forName("UTF8"));
//                return JSONObject.parseObject(result);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
//
//}
//}
